import fetch from 'dva/fetch';
import { stringify } from 'qs';
// import { stringify, parse } from 'qs';
// let str = parse("xyz=123&abc=456");
// let par = stringify({ xyz: 123, abc: 456 });

const FetchCall = async (url, type, data = {}) => {
    type = typeof (type) != "undefined" ? type.toLowerCase() : 'get';
    url = type === "get" ? (JSON.stringify(data) !== "{}" ? `${url}?${stringify(data)}` : url) : url;

    return await fetch(url, {
        method: type,
        ...(type === "get" ? {} : { body: JSON.stringify(data) }), //★☆☆☆★  get和HEAD 请求不能有 body★☆☆☆★
        cache: "no-store", //no-cache
        mode: 'cors',
        headers: {
            //'Authorization': window.localStorage["Token"],
            'Cache-Control': 'no-store',
            'Content-Type': 'application/json;charset=utf-8'
        }
    }).then(res => {
        if (res !== undefined) {
            if (res.status === 200)
                return res.json();  //arrayBuffer()、blob()、formData()、json()、text()
            else
                console.log(`Fetch异常状态码：${res.status}`);
        } else
            console.log("Axios Response is undefined");
    }).catch(error => {
        debugger;
        console.log("Fetch Catch：", error, JSON.stringify(error));
    });
}

//调用示例
//FetchCall("http://localhost:40301/api/Default1/Feach", "post", { username: "daea", password: "password" }).then(res => console.log(alert(res)));
//FetchCall("http://localhost:40301/api/Default1/Feach").then(res => console.log(alert(res)));

export default FetchCall;